<?php
/**
 * Description:账单相关
 * User: wh
 * Date: 2017-08-30
 * Time: 10:49
 */
namespace app\wxbackend\controller;
use app\wxbackend\controller\Wx\WxSession;
use app\wxbackend\model\BillManager;
use app\wxbackend\model\Building;
use app\wxbackend\model\Lease;
use think\Controller;

class BillManagerController extends Controller
{
    public function getLeaseBill()
    {
        $openid   = I('openid', '', 'strval');
        //$openid = 'o2-PovvddJrGJf_Tdei42OzHnf2o';
        $page     = I('page', 1, 'intval');
        $status   = I('status', 0, 'intval');
        $pageSize = I('page_size', 15, 'intval');
        //$openid = 'o2-PovnAE_kYfb_Xiobw4lhgM7k4';
        if (!$openid) {
            $this->apiReturn(400, [], '未定义openid');
        }

        //查询到本月底
        $end = date('Ym').date('t');
        WxSession::setSession($openid);
        if (!session('mid')) {
            $this->apiReturn(400, [], '未绑定平台账号');
        }

        $billModel = new BillManager();
        $buildModel = new Building();
        $leaseModel = new Lease();
        $res = $billModel->leaseBill(session('mid'), $end, $status, $page, $pageSize);
        if ($res) {
            $orderNums  = array_unique(array_column($res, 'ordernum'));

            //获取其他的基本信息
            $billInfo   = $billModel->getPayBillInfo(['ordernum' => ['in', $orderNums], 'is_delete' => 0], 'ordernum, building_id, charge_out, begin_cycle, status');
            $buildingId = array_unique(array_column($billInfo, 'building_id'));
            //获取楼宇名称
            $buildingInfo = $buildModel->getBuildingList(['id' => ['in', $buildingId]], 'id, name');
            //获取对应的房源信息
            $houseNumbers = $leaseModel->getLeaseExtByOrderNum($orderNums, 'ordernum, house_number');

            $houseInfo = [];
            foreach ($houseNumbers as $a => $house) {
                $houseInfo[] = $house['house_number'];
            }
            $houseList = $buildModel->getHouseList(['house_number' => ['in', $houseInfo]], 'house_number, room');
            foreach ($res as $k =>  $v) {
                //获取每条账单对应的账单id数组
                $orderId = $billModel->getBillDetailByOwner(session('mid'), session('pdtype'), $v['ordernum'], $v['begin_cycle'], $v['end_cycle'], $v['status'], 'id');
                $res[$k]['order_id'] = array_column($orderId, 'id');
                $res[$k]['total_money'] = yuan($v['total_money']);
                $res[$k]['room'] = '';
                //获取账单明细
                $detail = $billModel->leaseBillDetail($v['ordernum'], $v['begin_cycle'], $v['end_cycle']);
                $res[$k]['detail'] = $detail ?: [];
                //获取账单的对应的房源编号
                foreach ($houseNumbers as $number) {
                    if ($v['ordernum'] == $number['ordernum']) {
                        $res[$k]['house_number'][] = $number['house_number'];
                    }
                }
                //获取房源编号对应的房号
                foreach ($houseList as $h) {
                    if (in_array($h['house_number'], $res[$k]['house_number'])) {
                        $res[$k]['room'] .= $h['room'].',';
                    }
                }
                $res[$k]['room'] = rtrim($res[$k]['room'], ',');
                //获取账单的出账日期和楼宇id
                foreach ($billInfo as  $bill) {
                    if ($bill['ordernum'] == $v['ordernum'] && $bill['begin_cycle'] == $v['begin_cycle'] && $bill['status'] == $v['status']) {
                        $res[$k]['charge_out']  = $bill['charge_out'];
                        $res[$k]['building_id'] = $bill['building_id'];
                        break;
                    }
                }

                //获取对应的楼宇名称
                foreach ($buildingInfo as $build) {
                    if ($build['id'] == $res[$k]['building_id']) {
                        $res[$k]['name'] = $build['name'];
                        break;
                    }
                }
            }
            $sum   = $billModel->countLeaseBill(session('mid'), $end, $status);
            $total = ceil($sum / $pageSize);
            $data  = ['list' => $res, 'current_page' => $page, 'total' => $total];
            $this->apiReturn(200, $data, '');

        } else {
            $this->apiReturn(201, [], '没有数据');
        }

    }



}